package com.burgeon.backend.EnterpriseWeChat.service.Impl;

import com.burgeon.backend.EnterpriseWeChat.entity.*;
import com.burgeon.backend.EnterpriseWeChat.rest.Analysis.*;
import com.burgeon.backend.EnterpriseWeChat.rest.action.ActionRest;
import com.burgeon.backend.EnterpriseWeChat.service.EWAnalysisService;
import com.burgeon.backend.EnterpriseWeChat.service.ex.UpDataException;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;

@Service
public class EWAnalysisServiceImpl implements EWAnalysisService {
    private final static Log logger = LogFactory.getLog(EWAnalysisService.class);
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public VipRate getVipRate(String token, String storeId) {
        Info info = GetInfoByToken(token);
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        getVipRate getVipRate = new getVipRate(info.getUrl(),info.getAccount(),info.getPassword());
        VipRate vipRate = new VipRate();
        try {
            //
            actionRest.getData("VIP_RATESTORE_NO2_GENERATE");
            vipRate = getVipRate.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }

        return vipRate;
    }

    @Override
    public ArrayList<ChartSurvey> getChartData(String token, String storeId) {
        Info info = GetInfoByToken(token);
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        getChartData getChartData = new getChartData(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<ChartSurvey> chartSurveys = new ArrayList<>();
        try {
            actionRest.getData("VIP_RATESTORE_NO2_GENERATE");
            chartSurveys = getChartData.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }

        return chartSurveys;
    }

    @Override
    public HashMap getStoreKpi(String token, String storeId,String type) {
        Info info = GetInfoByToken(token);
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        getStoreKpi getStoreKpi = new getStoreKpi(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<StoreKpi> storeKpis = new ArrayList<>();
        HashMap map = new HashMap();
        try {
            actionRest.getData("T_SALE_SINGLE01_GENERATE");
            String column = "";
            switch (type) {
                case "1":
                    column = "TOT_COUNT_RETAIL";
                    break;
                case "2":
                     column = "LDL";
                    break;
                case "3":
                     column = "JDJ";
                    break;
                case "4":
                     column = "KDJ";
                    break;
                case "5":
                     column = "AVG_DISCOUNT";
                    break;
            }
            storeKpis = getStoreKpi.getData(storeId,column);
            ArrayList dateList = new ArrayList();
            ArrayList contentList = new ArrayList();
            for(int i =0;i < storeKpis.size();i++){
                dateList.add(storeKpis.get(i).getBillDate().substring(4,storeKpis.get(i).getBillDate().length()));
                contentList.add(storeKpis.get(i).getContent());
            }

            map.put("dateList",dateList);
            map.put("contentList",contentList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new UpDataException(e.getMessage());
        }
        return map;
    }

    @Override
    public ArrayList<VipRelegation> queryVipRelegation(String token, String storeId, String content, int page) {
        Info info = GetInfoByToken(token);
//        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        queryVipRelegation queryVipRelegation = new queryVipRelegation(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<VipRelegation> vipRelegations = new ArrayList<>();
        try {
            vipRelegations = queryVipRelegation.getData(storeId, content, page);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("升降级记录"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }
        return vipRelegations;
    }

    @Override
    public ArrayList<RFIM> getRfimData(String token, String storeId) {
        Info info = GetInfoByToken(token);
        getRfimData getRfimData = new getRfimData(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<RFIM> rfims= new ArrayList<>();
        try {
            rfims = getRfimData.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("RFIM占比"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }

        return rfims;
    }

    @Override
    public HashMap<String, ArrayList<String>> getStoreCRM(String token, String empId) {
        Info info = GetInfoByToken(token);
        getStoreCRM getStoreCRM = new getStoreCRM(info.getUrl(),info.getAccount(),info.getPassword());
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        HashMap<String, ArrayList<String>> map = new HashMap<>();
        try {
            actionRest.getData("S_BULLETINBOARD2_GENERATE");
           map = getStoreCRM.getData(empId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("同环比"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }

        return map;
    }

    @Override
    public ArrayList<StoreCRM> getStoreCRMItems(String token, String empId) {
        Info info = GetInfoByToken(token);
        getStoreCRMItems getStoreCRMItems = new getStoreCRMItems(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<StoreCRM> storeCRMS= new ArrayList<>();
        ActionRest actionRest = new ActionRest(info.getUrl(),info.getAccount(),info.getPassword());
        try {
            actionRest.getData("S_BULLETINBOARD2_GENERATE");
            storeCRMS = getStoreCRMItems.getData(empId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("同环比具体数据"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }
        return storeCRMS;
    }

    @Override
    public ArrayList<VipData> getVipTypeData(String token, String storeId, String type) {
        if (null == storeId || storeId.isEmpty()){
            throw new UpDataException("店铺id没有");
        }
        Info info = GetInfoByToken(token);
        //卡类型
        getVipTypeData getVipTypeData = new getVipTypeData(info.getUrl(),info.getAccount(),info.getPassword());
        //年龄
        getAgeData getAgeData = new getAgeData(info.getUrl(),info.getAccount(),info.getPassword());
        //性别
        getGenderData getGenderData = new getGenderData(info.getUrl(),info.getAccount(),info.getPassword());
        //星座
        getConstellation getConstellation = new getConstellation(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<VipData> chartSurveys= new ArrayList<>();
        try {
            if("1".equals(type)){
                chartSurveys = getVipTypeData.getData(storeId);
            }else if ("2".equals(type)){
                ArrayList<VipData> chartSurveyAge= new ArrayList<>();
                ArrayList<VipData> chartSurveyGender= new ArrayList<>();
                //年龄
                chartSurveyAge = getAgeData.getData(storeId);
                //性别
                chartSurveyGender = getGenderData.getData(storeId);

                chartSurveys.addAll(chartSurveyAge);
                chartSurveys.addAll(chartSurveyGender);

            }else if ("3".equals(type)){
                chartSurveys = getConstellation.getData(storeId);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("会员构成"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }
        return chartSurveys;
    }

    @Override
    public ArrayList<VipData> getAgeData(String token, String storeId) {
        Info info = GetInfoByToken(token);
        getAgeData getAgeData = new getAgeData(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<VipData> chartSurveys= new ArrayList<>();
        try {
            chartSurveys = getAgeData.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("年龄"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }
        return chartSurveys;
    }

    @Override
    public ArrayList<VipData> getGenderData(String token, String storeId) {
        Info info = GetInfoByToken(token);
        getGenderData getGenderData = new getGenderData(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<VipData> chartSurveys= new ArrayList<>();
        try {
            chartSurveys = getGenderData.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("年龄"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }
        return chartSurveys;
    }

    @Override
    public ArrayList<VipData> getConstellation(String token, String storeId) {
        Info info = GetInfoByToken(token);
        getConstellation getConstellation = new getConstellation(info.getUrl(),info.getAccount(),info.getPassword());
        ArrayList<VipData> chartSurveys= new ArrayList<>();
        try {
            chartSurveys = getConstellation.getData(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("年龄"+e.getMessage());
            throw new UpDataException(e.getMessage());
        }
        return chartSurveys;
    }

    private Info GetInfoByToken(String token) {
        return accountMapper.GetTokenData(token);
    }

}
